import time
import datetime


class DateTimeUtil():
    def __init__(self, statement=True):
        if statement: self.statement()

    def timeStamp2date(self, timeStamp=''):
        if timeStamp == '':
            timeStamp = int(time.time())
        # # 使用time
        # timeArray = time.localtime(timeStamp)
        # date = time.strftime("%Y-%m-%d", timeArray)

        # 使用datetime
        datetimeArray = datetime.datetime.fromtimestamp(timeStamp)
        date = datetimeArray.strftime("%Y-%m-%d")
        return date

    def timeStamp2datetime(self, timeStamp=''):
        if timeStamp == '':
            timeStamp = int(time.time())
        # # 使用time
        # timeArray = time.localtime(timeStamp)
        # date_time = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)

        # 使用datetime
        datetimeArray = datetime.datetime.fromtimestamp(timeStamp)
        date_time = datetimeArray.strftime("%Y-%m-%d %H:%M:%S")
        return date_time

    def datetime2timeStamp(self, date_time=''):
        if date_time == '':
            date_time = str(datetime.datetime.now())[:19]
        # 转为时间数组
        timeArray = time.strptime(date_time, "%Y-%m-%d %H:%M:%S")
        # 转为时间戳
        timeStamp = int(time.mktime(timeArray))
        return timeStamp

    def statement(self):
        statement = f'{self.__class__.__name__}\n' \
                         '作者: 梦辰雪（Cui Mengchao）\n' \
                         '版本号: v1.0.0\n' \
                         '更新时间: 2023-05-10\n' \
                         'gitee: https://gitee.com/mcxgitee\n'
        print(f'\033[1;33m{statement}\033[0m\n')


# region 单例
instance = None

def getInstance(statement=True):
    global instance
    if not instance:
        instance = DateTimeUtil(statement=statement)
    return instance
# endregion